<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class RechargeLog extends Base
{
    public function initialize()
    {
        parent::initialize();
        //钱包
        $wallet = logic('Wallet')->getName();
        $this->wallet = $wallet;
        View::assign('wallet',$wallet);
    }
    public function index(){
        //$map = $this->_search();
        $wallet_arr = logic('Wallet')->getName();
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['r.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['r.username'] = input('username');
            }
            if(input('money_type')){
                $map['r.money_type'] = input('money_type');
            }
            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['r.w_time'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('recharge_log')->alias('r')->where($map)->count();

            $field = 'r.*';
            $list = Db::name('recharge_log')
                ->alias('r')
                ->field($field)
                ->where($map)
                ->order('r.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $v['wallet_name'] = $wallet_arr[$v['money_type']];
                $list[$k] = $v;
            }
            $total = Db::name('recharge_log')
                ->alias('r')
                ->where($map)
                ->field('sum(money) number_total')
                ->find();
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list,'totalRow'=>$total]);
        }
        $level_arr = Db::name('user_level')->field('level_name,rank')->select();
        View::assign('level_arr',$level_arr);

        View::assign('wallet_arr',$wallet_arr);
        return View::fetch();
    }

    /*
     * 充值
     */
    public function recharge(){
        if (IS_POST) {
            $username		= I('username', 'trim');
            $number	= I('number', 'floatval');
            $money_type	= I('money_type', 'trim');
            $memo = I('memo', 'trim');

            if($number==0) {
                $this->error('充值金额不能等于0');
            }
            if(!$username) {
                $this->error('请输入会员手机号');
            }
            $user = Db::name('Users')->where(['username'=>$username,'is_lock'=>0])->find();
            if(!$user){
                $this->error('会员不存在或者已冻结');
            }

            Db::startTrans();
            //插入充值记录
            $data = [];
            $data['user_id'] = $user['user_id'];
            $data['username'] = $username;
            $data['money'] = $number;
            $data['admin_id'] = session('admin_id');
            $data['money_type'] = $money_type;
            $data['w_time'] = time();
            $data['memo'] = $memo;
            $rs = Db::name('recharge_log')->add($data);
            if(!$rs){
                Db::rollback();
                $this->error('插入充值记录失败');
            }

            //更新钱包
            $result=qianbao($user['user_id'],$number,$money_type,'管理员充单',$memo);
            if($result['status']){
                Db::commit();
                $this->success('充值成功！',U('index'));
            }else{
                Db::rollback();
                $this->error( $result['msg']);
            }
        }else{
            $username		= I('username','', 'trim');

            View::assign('username',$username);
            return View::fetch();
        }
    }

    /**
     * 导出
     */
    public function excel()
    {
        $wallet_arr = logic('Wallet')->getName();
        $page = input('page');
        $limit = input('limit')?:15;
        $map = [];
        if(input('user_id')){
            $map['r.user_id'] = input('user_id');
        }
        if(input('username')){
            $map['r.username'] = input('username');
        }
        if(input('money_type')){
            $map['r.money_type'] = input('money_type');
        }
        if(input('time_start') && input('time_end')){
            $time_start = strtotime(input('time_start'));
            $time_end = strtotime(input('time_end'));
            $map['r.w_time'] = ['between',[$time_start,$time_end]];
        }
        $field = 'r.*';
        $count = Db::name('recharge_log')
            ->alias('r')
            ->field($field)
            ->where($map)
            ->count();
        $list = Db::name('recharge_log')
            ->alias('r')
            ->field($field)
            ->where($map)
            ->page($page,$limit)
            ->order('r.id desc ')
            ->select();
        $title = ['ID','会员ID','会员编号','充值钱包','充值金额','备注','管理员ID','充值时间'];
        $data = [];
        foreach($list as $k=>$v){
            $v['wallet_name'] = $wallet_arr[$v['money_type']];
            $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
            $list[$k] = $v;
            $arr = [
                $v['id'],$v['user_id'],$v['username'],$v['wallet_name'],$v['money'],$v['memo'],$v['admin_id'],$v['w_time']
            ];
            $data[] = $arr;
        }
        return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'title'=>$title]);
        $this->export_excel($title,$data);
    }

    public function del(){
        $id_arr = input('ids/a');
        Db::startTrans();
        Db::name('recharge_log')->where(['id'=>['in',$id_arr]])->delete();
        Db::commit();
        return json(['status'=>1,'msg'=>'删除成功!']);
    }

    public function huikuan(){
        //$map = $this->_search();
        $wallet_arr = logic('Wallet')->getName();
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['r.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['r.username'] = input('username');
            }
            if(input('money_type')){
                $map['r.money_type'] = input('money_type');
            }
            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['r.addtime'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('huikuan')->alias('r')->where($map)->count();

            $field = 'r.*';
            $list = Db::name('huikuan')
                ->alias('r')
                ->field($field)
                ->where($map)
                ->order('r.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $v['wallet_name'] = $wallet_arr[$v['money_type']];
                $list[$k] = $v;
            }
            $total = Db::name('huikuan')
                ->alias('r')
                ->where($map)
                ->field('sum(money) number_total')
                ->find();
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list,'totalRow'=>$total]);
        }
        $level_arr = Db::name('user_level')->field('level_name,rank')->select();
        View::assign('level_arr',$level_arr);

        View::assign('wallet_arr',$wallet_arr);
        return View::fetch();
    }

    /**
     * 导出
     */
    public function excel_huikuan()
    {
        $wallet_arr = logic('Wallet')->getName();
        $page = input('page');
        $limit = input('limit')?:15;
        $map = [];
        if(input('user_id')){
            $map['r.user_id'] = input('user_id');
        }
        if(input('username')){
            $map['r.username'] = input('username');
        }
        if(input('money_type')){
            $map['r.money_type'] = input('money_type');
        }
        if(input('time_start') && input('time_end')){
            $time_start = strtotime(input('time_start'));
            $time_end = strtotime(input('time_end'));
            $map['r.addtime'] = ['between',[$time_start,$time_end]];
        }
        $count = Db::name('huikuan')->alias('r')->where($map)->count();

        $field = 'r.*';
        $list = Db::name('huikuan')
            ->alias('r')
            ->field($field)
            ->where($map)
            ->order('r.id desc ')
            ->page($page,$limit)
            ->select();
        foreach($list as $k=>$v){
            $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
            $v['wallet_name'] = $wallet_arr[$v['money_type']];
            $list[$k] = $v;
        }
        $title = ['ID','会员ID','会员编号','充值钱包','充值金额','打款人','打款时间','汇款截图','状态','备注'];
        $data = [];
        foreach($list as $k=>$v){
            $v['wallet_name'] = $wallet_arr[$v['money_type']];
            $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $status_str = '';
            if($v['status']==1){
                $status_str = '通过';
            }elseif($v['status']==2){
                $status_str = '拒绝';
            }else{
                $status_str = '未审核';
            }
            $list[$k] = $v;
            $arr = [
                $v['id'],$v['user_id'],$v['username'],$v['wallet_name'],$v['money'],$v['dk_name'],$v['addtime'],$v['original_img'],$status_str,$v['memo']
            ];
            $data[] = $arr;
        }
        return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'title'=>$title]);
        $this->export_excel($title,$data);
    }

    //同意汇款
    public function agrees(){
        $ids=I('post.id');
        if(!$ids) {
            return json(['status'=>0,'msg'=>'请选择同意提现的记录']);
        }
        Db::startTrans();
        $modtime=time();
        $map=array('id'=>array('in',$ids),'status'=>0);
        $list = Db::name('huikuan')->where($map)->select();
        if(!$list){
            return json(['status'=>0,'msg'=>'状态有误']);
        }
        Db::name('huikuan')->where($map)->save(array('status'=>1,'edittime'=>$modtime));
        foreach($list as $key=>$val) {
            $result = qianbao($val['user_id'], $val['money'], $val['money_type'], '线下汇款', '线下汇款');
            if (!$result['status']) {
                Db::rollback();
                return json(['status' => 0, 'msg' => $result['msg']]);
            }
        }
        Db::commit();
        return json(['status'=>1,'msg'=>'操作成功']);
    }
    //拒绝汇款
    public function refunds(){
        $ids=I('post.id');
        if(!$ids) {
            return json(['status'=>0,'msg'=>'请选择拒绝汇款的记录']);
        }
        $modtime=time();

        $map = array('id'=>array('in',$ids),'status'=>0);
        $list = Db::name('huikuan')->where($map)->select();
        if(!$list){
            return json(['status'=>0,'msg'=>'状态有误']);
        }
        Db::startTrans();
        $reason = input('reason');
        foreach($list as $key=>$val) {
            $rs = Db::name('huikuan')->where(array('id'=>$val['id']))->save(array('status'=>2,'edittime'=>$modtime,'memo'=>$reason));
            if(!$rs){
                Db::rollback();
                return json(['status'=>0,'msg'=>'更新状态失败']);
            }
        }
        Db::commit();
        return json(['status'=>1,'msg'=>'拒绝汇款成功']);
    }
}